﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.Common;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// 有机硫。编辑
    /// </summary>
    /// 创建时间：2022-12-27 15:57:42。作者：王浩力
    public partial class WindowsSulfurEdit : Window
    {
        public WindowsSulfurEdit()
        {
            InitializeComponent();
        }
        ISource_dictDAL source_DictDAL = ServicesHelpter.GetService<ISource_dictDAL>();
        SulfurBusiness business = new SulfurBusiness();
        UserBusiness userBusiness = new UserBusiness();

        /// <summary>
        /// 刷新父页面分页数据
        /// </summary>
        internal event EventHandler RefreshPage;

        /// <summary>
        /// 被编辑的 Organic_sulfur
        /// </summary>
        internal Organic_sulfur editInfo;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.Tag != null && this.Tag.Equals("jiaodui"))
            {
                this.Title = "有机硫校对";
                btnConfirm.Content = "确认校对";
            }

            var all = source_DictDAL.GetAll();
            all = all.FindAll(g => g.D_stat == 1);

            //人 。添加时间：2023-1-9 16:44:48
            var allUser = userBusiness.GetAllUser();
            allUser = allUser.FindAll(g => g.U_stat == 1);

            //取样条件
            cmb_Sampling_conditions.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyangCondition"));
            cmb_Sampling_conditions.DisplayMemberPath = "Item_name";
            cmb_Sampling_conditions.SelectedValuePath = "Id";

            //委托单位
            cmb_Client.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyang"));
            cmb_Client.DisplayMemberPath = "Item_name";
            cmb_Client.SelectedValuePath = "Id";

            //取样人
            cmb_Sampling_user_name.ItemsSource = allUser;
            cmb_Sampling_user_name.DisplayMemberPath = "User_real_name";
            cmb_Sampling_user_name.SelectedValuePath = "Id";

            //分析人   //添加时间：2023-1-9 16:45:45。
            cmb_analysis_user.ItemsSource = allUser;
            cmb_analysis_user.DisplayMemberPath = "User_real_name";
            cmb_analysis_user.SelectedValuePath = "Id";

            //取样单位
            cmb_Sampling_unit.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyang"));
            cmb_Sampling_unit.DisplayMemberPath = "Item_name";
            cmb_Sampling_unit.SelectedValuePath = "Id";

            //层位
            cmb_Horizon.ItemsSource = all.FindAll(g => g.Group_id.Equals("cengwei"));
            cmb_Horizon.DisplayMemberPath = "Item_name";
            cmb_Horizon.SelectedValuePath = "Id";

            //分析单位
            cmb_Analysis_unit.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyang"));
            cmb_Analysis_unit.DisplayMemberPath = "Item_name";
            cmb_Analysis_unit.SelectedValuePath = "Id";

            //取样位置
            cmb_Sampling_location.ItemsSource = all.FindAll(g => g.Group_id.Equals("quyangLocation"));
            cmb_Sampling_location.DisplayMemberPath = "Item_name";
            cmb_Sampling_location.SelectedValuePath = "Id";

            //--------------------------------------数据编辑赋值--------------------------------------
            //日期时间初始化
            date_Analysis_time.SelectedDateTime = Convert.ToDateTime(editInfo.Analysis_time);
            date_Sampling_time.SelectedDateTime = Convert.ToDateTime(editInfo.Sampling_time);
            txtSamp_no.Text = editInfo.Samp_no;
            txtWell_deep.Text = editInfo.Well_deep.ToString();

            cmb_Sampling_conditions.SelectedValue = editInfo.Sampling_conditions ?? null;
            cmb_Client.SelectedValue = editInfo.Client ?? null;
            cmb_Sampling_user_name.SelectedValue = editInfo.Sampling_user_name ?? null;
            cmb_Sampling_unit.SelectedValue = editInfo.Sampling_unit ?? null;

            txtWell_no.Text = editInfo.Well_no;
            if (!string.IsNullOrWhiteSpace(editInfo.Horizon))
            {
                cmb_Horizon.SelectedValue = editInfo.Horizon;
            }

            txtOil_pressure.Text = editInfo.Oil_pressure.ToString();
            cmb_Analysis_unit.SelectedValue = editInfo.Analysis_unit ?? null;

            //取样位置；修改时间：2023-1-15 21:26:14。
            if (!string.IsNullOrWhiteSpace(editInfo.Sampling_location))
            {
                if (Regex.IsMatch(editInfo.Sampling_location,"^\\d{1,19}"))
                {
                    cmb_Sampling_location.SelectedValue = editInfo.Sampling_location;
                }
                else
                {
                    txt_Sampling_location.Text = editInfo.Sampling_location;
                }             
            }

            txtSampling_temp.Text = editInfo.Sampling_temp.ToString();
            txtCasing_pressure.Text = editInfo.Casing_pressure.ToString();

            s_Cos.Text = editInfo.Cos.ToString();
            s_Dimethyl_Sulfide.Text = editInfo.Dimethyl_Sulfide.ToString();
            s_Tert_butyl.Text = editInfo.Tert_butyl.ToString();
            s_Diethyl_sulphide.Text = editInfo.Diethyl_sulphide.ToString();

            s_Methyl_mercaptan.Text = editInfo.Methyl_mercaptan.ToString();
            s_Carbon_disulfide.Text = editInfo.Carbon_disulfide.ToString();
            s_Methylethyl_sulfide.Text = editInfo.Methylethyl_sulfide.ToString();
            s_N_Butyl_mercaptan.Text = editInfo.N_Butyl_mercaptan.ToString();

            s_Ethanethiol.Text = editInfo.Ethanethiol.ToString();
            s_Isopropanethiol.Text = editInfo.Isopropanethiol.ToString();
            s_Thiophene.Text = editInfo.Thiophene.ToString();
            s_Dithioether.Text = editInfo.Dithioether.ToString();

            //有机硫总含量 mg/m3
            s_total.Content = editInfo.Organic_sulfur_total.ToString();
            txtRemark.Text = editInfo.Remark;

            //分析人id；添加时间：2023-1-13 12:00:18。
            cmb_analysis_user.SelectedValue = editInfo.Analysis_userID;

            date_Analysis_time.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
        }

        //保存
        private void btnConfirm_Click(object sender, RoutedEventArgs e)
        {
            //取样位置  //修改时间：2023-1-15 21:22:35
            string sampLocation = cmb_Sampling_location.SelectedValue?.ToString();
            if (!string.IsNullOrWhiteSpace(txt_Sampling_location.Text))
            {
                sampLocation = txt_Sampling_location.Text.Trim();
            }

            Organic_sulfur organic_Sulfur = new Organic_sulfur();
            organic_Sulfur.Samp_no = txtSamp_no.Text.Trim();
            organic_Sulfur.Well_deep = txtWell_deep.Text.Trim();
            organic_Sulfur.Sampling_conditions = cmb_Sampling_conditions.SelectedValue?.ToString();
            organic_Sulfur.Client = cmb_Client.SelectedValue?.ToString();
            organic_Sulfur.Sampling_user_name = cmb_Sampling_user_name.SelectedValue?.ToString();

            organic_Sulfur.Sampling_unit = cmb_Sampling_unit.SelectedValue?.ToString();
            organic_Sulfur.Well_no = txtWell_no.Text.Trim();
            organic_Sulfur.Horizon = cmb_Horizon.SelectedValue?.ToString();
            organic_Sulfur.Oil_pressure = txtOil_pressure.Text.Trim().ToMyDecimal();
            organic_Sulfur.Sampling_time = date_Sampling_time.Text;
            organic_Sulfur.Analysis_time = date_Analysis_time.Text;

            organic_Sulfur.Analysis_unit = cmb_Analysis_unit.SelectedValue?.ToString();

            //organic_Sulfur.Sampling_location = cmb_Sampling_location.SelectedValue?.ToString();
            organic_Sulfur.Sampling_location = sampLocation;

            organic_Sulfur.Sampling_temp = txtSampling_temp.Text.ToMyDecimal();
            organic_Sulfur.Casing_pressure = txtCasing_pressure.Text.ToMyDecimal();

            organic_Sulfur.Cos = s_Cos.Text.ToMyDecimal();
            organic_Sulfur.Dimethyl_Sulfide = s_Dimethyl_Sulfide.Text.ToMyDecimal();
            organic_Sulfur.Tert_butyl = s_Tert_butyl.Text.ToMyDecimal();
            organic_Sulfur.Diethyl_sulphide = s_Diethyl_sulphide.Text.ToMyDecimal();

            organic_Sulfur.Methyl_mercaptan = s_Methyl_mercaptan.Text.ToMyDecimal();
            organic_Sulfur.Carbon_disulfide = s_Carbon_disulfide.Text.ToMyDecimal();
            organic_Sulfur.Methylethyl_sulfide = s_Methylethyl_sulfide.Text.ToMyDecimal();
            organic_Sulfur.N_Butyl_mercaptan = s_N_Butyl_mercaptan.Text.ToMyDecimal();

            organic_Sulfur.Ethanethiol = s_Ethanethiol.Text.ToMyDecimal();
            organic_Sulfur.Isopropanethiol = s_Isopropanethiol.Text.ToMyDecimal();
            organic_Sulfur.Thiophene = s_Thiophene.Text.ToMyDecimal();
            organic_Sulfur.Dithioether = s_Dithioether.Text.ToMyDecimal();
            organic_Sulfur.Remark = txtRemark.Text;//备注

            //有机硫总含量mg / m³
            organic_Sulfur.Organic_sulfur_total = s_total.Content.ToMyDecimal();

            //分析人。//添加时间：2023-1-9 16:56:36。作者：王浩力
            organic_Sulfur.Analysis_userID = cmb_analysis_user.SelectedValue.ToMyInt64();

            organic_Sulfur.Id = editInfo.Id;

            IResult result = null;
            if (this.Tag != null && this.Tag.Equals("jiaodui"))
            {
                //校对
                result = business.Proofread(organic_Sulfur);
            }
            else
            {
                //编辑
                result = business.Edit(organic_Sulfur);
            }

            if (result.CodeEnum == ResultCode.Success)
            {
                btnConfirm.IsEnabled = false;
                RefreshPage?.Invoke(this, e);
            }

            ShowAlertHelpter.ShowAlert(result.Msg);
        }



        //退出
        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //检查输入字符串是否为数字 //创建时间：2022-12-14 14:10:10，作者：王浩力
        private void numberCheck_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            var textElem = (sender as TextBox);

            //解决连续输入很多.
            if ((
                textElem.Text.StartsWith(".") || string.IsNullOrWhiteSpace(textElem.Text)
                || textElem.Text.Contains(".")
                ) && e.Text == ".")
            {
                e.Handled = true;
                return;
            }

            //禁止在0前面继续输入0
            int index = ((System.Windows.Controls.TextBox)e.Source).CaretIndex;
            if (textElem.Text.StartsWith("0.") && index == 0 && e.Text == "0")
            {
                e.Handled = true;
                return;
            }

            //不能输入非数字
            if (Regex.IsMatch(e.Text, @"[^\d+\.{0,1}\d+]+"))
            {
                e.Handled = true;
                return;
            }
        }

        //检查输入字符串是否为数字 //创建时间：2022-12-14 14:10:02，作者：王浩力
        private void numberCheck_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            TextBox textBox = sender as TextBox;
            if (!string.IsNullOrWhiteSpace(textBox.Text))
            {
                if (textBox.Text == "0.0" || textBox.Text.StartsWith("00"))
                {
                    textBox.Text = textBox.Text.Replace("00", "");
                }
                if (!textBox.Text.StartsWith("0.") && Regex.IsMatch(textBox.Text, @"0+[1-9]+\.*\d+", RegexOptions.IgnoreCase))
                {
                    textBox.Text = textBox.Text.TrimStart('0');
                }

                //输入的不是小数或正整数           
                Regex regex = new Regex(@"[^\d+\.{0,1}\d+]", RegexOptions.IgnoreCase);
                textBox.Text = regex.Replace(textBox.Text, "");
            }
        }


        //计算总值，有机硫总含量mg/m³
        private void s_Cos_KeyUp(object sender, KeyEventArgs e)
        {
            //var textbox = sender as TextBox;
            //decimal input = textbox.Text.ToMyDecimal();
            decimal? total =
            +s_Cos.Text.ToMyDecimal2()
            + s_Dimethyl_Sulfide.Text.ToMyDecimal2()
            + s_Tert_butyl.Text.ToMyDecimal2()
            + s_Diethyl_sulphide.Text.ToMyDecimal2()

            + s_Methyl_mercaptan.Text.ToMyDecimal2()
            + s_Carbon_disulfide.Text.ToMyDecimal2()
            + s_Methylethyl_sulfide.Text.ToMyDecimal2()
            + s_N_Butyl_mercaptan.Text.ToMyDecimal2()

            + s_Ethanethiol.Text.ToMyDecimal2()
            + s_Isopropanethiol.Text.ToMyDecimal2()
            + s_Thiophene.Text.ToMyDecimal2()
            + s_Dithioether.Text.ToMyDecimal2();

            s_total.Content = total.ToString();
        }

    }
}
